﻿package com.zjkj.mp.utils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.web.multipart.MultipartFile;

/**
 * 
 * 对上传的复合类型的文件进行处理
 * 
 * @author <a href="mailto:409384898@qq.com">陈晨</a>
 * @history
 * <TABLE id="HistoryTable" border="1">
 * 	<TR><TD>时间</TD><TD>描述</TD><TD>作者</TD></TR>
 *	<TR><TD>2012-9-23</TD><TD>创建初始版本</TD><TD>陈晨</TD></TR>
 * </TABLE>
 */
public class MulpartRequestWrapper extends HttpServletRequestWrapper {

	private Map<String, String[]> map;
	private String path; //全路径
	private String prefix; //文件名
	private String suffix; //文件后缀
	private String alias; //用户id+日期+id_时间.swf
	private String isOnline = "0";
	private String swfPath; //swf文件的全路径
	private String rootPath; //platform/upload
	
	/**
	 * 获取复杂类型的file文件处理
	 * @param request
	 * @param fileName
	 * @param uuid
	 * @param file
	 */
	@SuppressWarnings({"deprecation"})
	public MulpartRequestWrapper(HttpServletRequest request, String fileName, String uuid, MultipartFile file) {
		
		//继承不同的request特性
		super(request);
		try{
			boolean isMultpart = ServletFileUpload.isMultipartContent(request);
			if(isMultpart){
				map = new HashMap<String, String[]>();
				path = request.getRealPath("/") + "upload" + File.separator;
				rootPath = path;
				String time = new Toolkit().getSystemTime();
				String typeName = fileName.split("\\.")[1];
				if (!(("doc".equals(typeName))||("docx".equals(typeName))||("pdf".equals(typeName)))) {
					request.setAttribute("MSG", "导入的文件格式不正确，请重新导入");
				}
				if("".equals(fileName)){
					System.out.println("文件名为空");
				}else{
					prefix = fileName.split("\\.")[0];
					suffix = fileName.split("\\.")[1];
					fileName = prefix + "_" + time.substring(8) + "." + suffix;
					path += uuid + File.separator + time.substring(0,8) + File.separator; 
					File targetFile = new File(path, fileName);
					path += fileName;
					if (!targetFile.exists()) {
						targetFile.mkdirs();
					}
					// 保存
					try {
						file.transferTo(targetFile);
					} catch (Exception e) {
						e.printStackTrace();
					}
					if("doc".equalsIgnoreCase(suffix) || 
							"docx".equalsIgnoreCase(suffix) || 
							"pdf".equalsIgnoreCase(suffix)){
						alias = uuid + File.separator + time.substring(0,8) + 
								File.separator + prefix + "_" + time.substring(8) + ".swf";
						isOnline = "1";
						swfPath = rootPath + alias;
						alias = alias.replace("\\", "/");
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	@Override
	public String getParameter(String name) {
		String[] values = (String[]) map.get(name);
		if(values != null){
			return values[0];
		}
		return null;
	}
	@Override
	public String[] getParameterValues(String name) {
		String[] values = (String[]) map.get(name);
		return values;
	}
	@Override
	public Map<String, String[]> getParameterMap() {
		return map;
	}
	@Override
	public String toString(){
		return this.path + "|" + prefix + "|" + suffix + "|" + alias + "|" + isOnline + "|" + swfPath;
	}
}
